const pool = require("../db/conn");

exports.selectList = async function () {    
    let sql =   " SELECT "+
                " 	rc_admin.admin_id,  "+
                " 	rc_admin.admin_username,  "+
                " 	rs_role.role_id,  "+
                " 	rs_role.role_name,  "+
                " 	COUNT(rs_role_right.right_id) AS rights_count "+
                " FROM "+
                " 	rc_admin "+
                " 	LEFT JOIN "+
                " 	rs_role "+
                " 	ON  "+
                " 		rc_admin.admin_role_id = rs_role.role_id "+
                " 	LEFT JOIN "+
                " 	rs_role_right "+
                " 	ON  "+
                " 		rs_role.role_id = rs_role_right.role_id "+
                " WHERE "+
                "   rs_role.role_system != -1 "+
                " GROUP BY "+
                " 	rc_admin.admin_id ";
   
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql);
    conn.release();
    return result;
}

exports.insert = async function (admin_username,admin_role_id) {
    let sql = "INSERT INTO rc_admin (admin_username, admin_password, admin_role_id) VALUES (?, ?, ?)";

    let conn = await pool.getConnection();
    let [{changedRows}] = await conn.execute(sql,[admin_username,"123456",admin_role_id]);
    conn.release();
    
    return changedRows!=0;
}

exports.selectById = async function (admin_id) {
    let sql =   " SELECT "+
                " 	rc_admin.admin_id,  "+
                " 	rc_admin.admin_username,  "+
                "   rc_admin.admin_role_id "+
                " FROM "+
                " 	rc_admin "+
                " WHERE rc_admin.admin_id = ? ";
    
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql,[admin_id]);
    conn.release();
    
    return result[0];
}

exports.selectByName = async function (admin_username,admin_id) {
    let sql =   " SELECT "+
                " 	rc_admin.admin_id,  "+
                " 	rc_admin.admin_username,  "+
                "   rc_admin.admin_role_id "+
                " FROM "+
                " 	rc_admin "+
                " WHERE rc_admin.admin_username = ? ";

    let params = [admin_username];
    if(admin_id!=null&&admin_id!=""){
        sql += " AND rc_admin.admin_id != ? "
        params.push( admin_id );
    }
    
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql,params);
    conn.release();
    
    return result[0];
}

exports.updateById = async function (admin_id,admin_username,admin_role_id) {
    let sql = "UPDATE rc_admin SET admin_username = ?, admin_role_id = ? WHERE admin_id = ?";
    
    let conn = await pool.getConnection();
    let [{changedRows}] = await conn.execute(sql,[admin_username,admin_role_id,admin_id]);
    conn.release();
    
    return changedRows!=0;
}

exports.deleteByDid = async function (admin_id) {
    let sql =  "DELETE FROM rc_admin WHERE admin_id = ?";

    let conn = await pool.getConnection();
    let [{changedRows}] = await conn.execute(sql,[admin_id]);
    conn.release();
    
    return changedRows!=0;
}
